package org.springframework.prospring.ticket.web.views;

import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.View;
import org.springframework.prospring.ticket.domain.location.Region;
import org.springframework.prospring.ticket.domain.location.City;
import org.springframework.prospring.ticket.domain.Club;
import org.springframework.prospring.ticket.domain.ui.Response;
import org.springframework.prospring.ticket.web.UserContext;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver;

public class JsonView implements View {

    public static final JsonView instance = new JsonView();
    public static final String JSON_ROOT = "root";

    // This instance of XStream is thread-safe.  XStream is not
    // thread-safe if auto-detection of annotations is used.
    private XStream xstream = new XStream(new JettisonMappedXmlDriver());

    private JsonView() {
	// in a large project, listing every class with XStream
	// annotations is not practical.  2 alternatives are:
	//   * traversing the classpath using a specified root package
	//     and calling processAnnotations
	//   * enabling annotation auto-detection, but then the XStream
	//     instance is not thread-safe
        xstream.processAnnotations(City.class);
        xstream.processAnnotations(Region.class);
        xstream.processAnnotations(Club.class);
        xstream.processAnnotations(Response.class);
        xstream.processAnnotations(UserContext.class);

        /* esto es codigo que estaba en LINK
		xstream.setMode(XStream.NO_REFERENCES);
		xstream.autodetectAnnotations(true);
//		xstream.processAnnotations(String.class);
//		xstream.processAnnotations(MessageResponseModel.class);
//		xstream.processAnnotations(Usuario.class);
//		xstream.processAnnotations(CuentaAsociada.class);
//		xstream.processAnnotations(UsuarioDTO.class);
//		xstream.processAnnotations(ConsultaSaldo.class);
//		xstream.processAnnotations(GridDTO.class);
//		xstream.processAnnotations(GridRowDTO.class);
        
         */
    }

    public String getContentType() {
	return "text/html; charset=UTF-8";
    }

    @SuppressWarnings("unchecked")
    public void render(Map model, HttpServletRequest request,
	    HttpServletResponse response) throws Exception {
	Object root = model.get(JsonView.JSON_ROOT);
	if (root == null) {
	    throw new RuntimeException("JSON root object with key '"
		    + JsonView.JSON_ROOT + "' not found in model");
	}
	PrintWriter writer = response.getWriter();
	// even though the following method is "toXML" XStream is configured
	// to use Jettison to output JSON instead
	String json = xstream.toXML(root);
    System.out.println("================  JsonView.render json: " + json);
	writer.write(json);
    }

}
